package com.service.device;

import com.dao.secondary.device.Device25ObdInfoMapper;
import com.dao.secondary.device.DeviceObdBaseInfoMapper;
import com.dao.secondary.device.DeviceObdExFactoryBatchInfoMapper;
import com.model.domain.device.GetDevice25ObdInfoDO;
import com.model.domain.device.GetDeviceObdBaseInfoDO;
import com.model.domain.device.GetDeviceObdExFactoryBatchInfoDO;
import com.model.pojo.device.Device25ObdInfo;
import com.model.pojo.device.DeviceObdBaseInfo;
import com.model.pojo.device.DeviceObdExFactoryBatchInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.github.pagehelper.PageHelper;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;


/**
 * @Author: zql
 * @Description: 【请填写功能名称】服务
 * @Data: 2023-06-08
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class Device25ObdInfoService {
    private final Device25ObdInfoMapper device25ObdInfoMapper;
    private final DeviceObdExFactoryBatchInfoService deviceObdExFactoryBatchInfoService;
    private final DeviceObdBaseInfoService deviceObdBaseInfoService;
    public List<Device25ObdInfo> getPageList(GetDevice25ObdInfoDO getDevice25ObdInfoDO){
        PageHelper.clearPage();
        PageHelper.startPage(getDevice25ObdInfoDO.getPage(), getDevice25ObdInfoDO.getPageSize(), getDevice25ObdInfoDO.getSortRow() + getDevice25ObdInfoDO.getSortOrder());
        return device25ObdInfoMapper.selectList(getDevice25ObdInfoDO);
    }

    public List<Device25ObdInfo> getAllList(GetDevice25ObdInfoDO getDevice25ObdInfoDO){
        PageHelper.clearPage();
        PageHelper.orderBy( getDevice25ObdInfoDO.getSortRow() + getDevice25ObdInfoDO.getSortOrder());
        return device25ObdInfoMapper.selectList(getDevice25ObdInfoDO);
    }
    public List<Map<String,Object>> getListGroupBy(GetDevice25ObdInfoDO getDevice25ObdInfoDO){
        return device25ObdInfoMapper.selectListGroupBy(getDevice25ObdInfoDO);
    }
    public int countDtu(GetDevice25ObdInfoDO getDevice25ObdInfoDO){
        return device25ObdInfoMapper.countByDtu(getDevice25ObdInfoDO);
    }

    public List<Map<String,Object>> getObdInfoNumByDay(String date){
        GetDeviceObdExFactoryBatchInfoDO getDeviceObdExFactoryBatchInfoDO = new GetDeviceObdExFactoryBatchInfoDO();
        getDeviceObdExFactoryBatchInfoDO.setExFactory(1);
        getDeviceObdExFactoryBatchInfoDO.setSortRow("batchId");
        List<DeviceObdExFactoryBatchInfo> batchList = deviceObdExFactoryBatchInfoService.getAllList(getDeviceObdExFactoryBatchInfoDO);
        List<Long> batchIds = batchList.stream().map(item->item.getBatchId()).collect(Collectors.toList());

        GetDeviceObdBaseInfoDO getDeviceObdBaseInfoDO = new GetDeviceObdBaseInfoDO();
        getDeviceObdBaseInfoDO.setBatchIds(batchIds);
        getDeviceObdBaseInfoDO.setSortRow("id");
        List<DeviceObdBaseInfo> baseList = deviceObdBaseInfoService.getAllList(getDeviceObdBaseInfoDO);
        List<String> dtuList = baseList.stream().map(item->item.getDtu()).collect(Collectors.toList());
        StringBuffer dtus = new StringBuffer("");
        for(int i =0;i<baseList.size();i++){
            DeviceObdBaseInfo baseInfo = baseList.get(i);
            dtus.append("'"+baseInfo.getDtu()+"'");
            if(i!=baseList.size()-1)dtus.append(",");
        }
        GetDevice25ObdInfoDO getDevice25ObdInfoDO = new GetDevice25ObdInfoDO();
        getDevice25ObdInfoDO.setTableName("device_25_obd_info_"+date);
        getDevice25ObdInfoDO.setSortRow("dtu");
        getDevice25ObdInfoDO.setDtuList(dtuList);
        PageHelper.clearPage();
        PageHelper.orderBy(getDevice25ObdInfoDO.getSortRow()+getDevice25ObdInfoDO.getSortOrder());
        return device25ObdInfoMapper.selectListGroupBy(getDevice25ObdInfoDO);
    }
}
